package com.kime.email.book;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.FileOutputStream;
import java.util.Properties;

/**
 * Java 邮件开发详解
 * 创建一封包含图片和附件的复杂邮件
 */
public class ComplexMessage {
    public static void main(String[] args) throws Exception {
        Session session = Session.getDefaultInstance(new Properties());
        MimeMessage message = createMessage(session);
        message.writeTo(new FileOutputStream("D:\\workspace\\email-demo\\src\\main\\resources\\eml\\complexMessage.eml"));
    }

    public static MimeMessage createMessage(Session session) throws Exception {
        String from = "kimewen@163.com";
        String to   = "kimewen@qq.com";
        String subject = "HTML 邮件";
        String body = "<h4>欢迎大家阅读此邮件</h4>" +
                "<a href='https://www.baidu.com'>访问百度</a></br>" +
                "<img src=\"https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png\"><br/>" +
                "<p>这是一张图片</p>" +
                "<img src=\"cid:logo_oppo\"";

        MimeMessage mimeMessage = new MimeMessage(session);
        mimeMessage.setFrom(new InternetAddress(from));
        mimeMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        mimeMessage.setSubject(subject);

        // 创建代表邮件正文和附件的各个 MimeBodyPart 对象
        MimeBodyPart contentPart = createContent(body, "D:\\workspace\\email-demo\\src\\main\\resources\\image\\oppo.png");
        MimeBodyPart attachment1 = createAttachment("D:\\workspace\\email-demo\\src\\main\\resources\\txt\\file.txt");
        MimeBodyPart attachment2 = createAttachment("D:\\workspace\\email-demo\\src\\main\\resources\\txt\\file1.txt");

        // 创建用于组合邮件正文和附件的 MimeMultipart 对象
        MimeMultipart mimeMultipart = new MimeMultipart("mixed");
        mimeMultipart.addBodyPart(contentPart);
        mimeMultipart.addBodyPart(attachment1);
        mimeMultipart.addBodyPart(attachment2);

        // 设置整个邮件内容为最终组合出的 MimeMultipart 对象
        mimeMessage.setContent(mimeMultipart);
        mimeMessage.saveChanges();
        return mimeMessage;
    }

    public static MimeBodyPart createContent(String body, String filename) throws Exception {
        // 创建代表组合 MIME 消息的 mimeMultipart 对象，和将该 MimeMultipart 对象保存到的 MimeBodyPart 对象
        MimeBodyPart contentBodyPart = new MimeBodyPart();
        MimeMultipart contentMultipart = new MimeMultipart("related");

        // 创建用于保存 HTML 正文的 MimeBodyPart 对象，并将它保存到 MimeMultipart 中
        MimeBodyPart htmlBodyPart = new MimeBodyPart();
        htmlBodyPart.setContent(body, "text/html;charset=gb2312");
        contentMultipart.addBodyPart(htmlBodyPart);

        // 创建用于保存图片的 MimeBodyPart 对象，并将它保存到 MimeMultipart 中
        MimeBodyPart pictureBodyPart = new MimeBodyPart();
        FileDataSource fileDataSource = new FileDataSource(filename);
        pictureBodyPart.setDataHandler(new DataHandler(fileDataSource));
        pictureBodyPart.setFileName(fileDataSource.getName());
        pictureBodyPart.setContentID("logo_oppo");
        contentMultipart.addBodyPart(pictureBodyPart);

        // 将 MimeMultipart 对象保存到的 MimeBodyPart 对象中
        contentBodyPart.setContent(contentMultipart);
        return contentBodyPart;
    }

    public static MimeBodyPart createAttachment(String filename) throws Exception {
        // 创建保存附件的 MimeBodyPart 对象，并加入附件内容和相应信息
        MimeBodyPart attachBodyPart = new MimeBodyPart();
        FileDataSource fileDataSource = new FileDataSource(filename);
        attachBodyPart.setDataHandler(new DataHandler(fileDataSource));
        attachBodyPart.setFileName(fileDataSource.getName());
        return attachBodyPart;
    }
}
